package com.wuba.rxjavademo.tzx.lift;

import android.util.Log;

import com.wuba.rxjavademo.tzx.common.Base;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.functions.Func1;
import rx.subjects.PublishSubject;

/**
 * Created by Tanzhenxing
 * Date: 2019/1/9 4:06 PM
 * Description:
 */
public class RxtoList extends Base {
    public static void main(String[] args) {
        RxtoList rxtoList = new RxtoList();
        rxtoList.run();
    }

    static class MySubscriber extends Subscriber<Object> {

        @Override
        public void onCompleted() {
            System.out.println("onCompleted");
        }

        @Override
        public void onError(Throwable e) {
            System.out.println("onError");
        }

        @Override
        public void onNext(Object o) {
            System.out.println("onNext");
            System.out.println(o.toString());
        }
    }

    private MySubscriber sub = new MySubscriber();


    public static class OnSubscribe implements Observable.OnSubscribe<Object> {

        @Override
        public void call(Subscriber<? super Object> subscriber) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        if (!subscriber.isUnsubscribed()) {
                            subscriber.onNext(1);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    @Override
    public void run() {
        Observable observable = Observable.create(new OnSubscribe());
        Subscription subscription = observable.subscribe(sub);
        subscription.unsubscribe();
        System.out.println(subscription.isUnsubscribed());
    }
}
